package org.example.controller;

import io.swagger.annotations.*;
import org.example.model.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/api/users")
public class UserController {

    private List<User> users = new ArrayList<>();

    @ApiOperation(value = "创建用户", notes = "创建一个新的用户")
    @ApiResponses({
        @ApiResponse(code = 200, message = "创建成功"),
        @ApiResponse(code = 400, message = "请求参数错误")
    })
    @PostMapping
    public User createUser(@ApiParam(value = "用户信息", required = true) @RequestBody User user) {
        // 模拟保存用户
        user.setId(System.currentTimeMillis());
        users.add(user);
        return user;
    }

    @ApiOperation(value = "获取用户列表", notes = "获取所有用户的信息")
    @GetMapping
    public List<User> getAllUsers() {
        return users;
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return users.stream()
                .filter(user -> user.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    @ApiOperation(value = "更新用户", notes = "根据用户ID更新用户信息")
    @PutMapping("/{id}")
    public User updateUser(
            @ApiParam(value = "用户ID", required = true) @PathVariable Long id,
            @ApiParam(value = "用户信息", required = true) @RequestBody User user) {
        users.removeIf(u -> u.getId().equals(id));
        user.setId(id);
        users.add(user);
        return user;
    }

    @ApiOperation(value = "删除用户", notes = "根据用户ID删除用户")
    @DeleteMapping("/{id}")
    public void deleteUser(@ApiParam(value = "用户ID", required = true) @PathVariable Long id) {
        users.removeIf(user -> user.getId().equals(id));
    }
} 